package com.yidiandao.business.ui.widget.recyclerview.provider;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.yidiandao.business.R;
import com.yidiandao.business.data.recyclertype.AccurateContent;
import com.yidiandao.business.data.recyclertype.ArticleTitle;
import com.yidiandao.business.ui.main.discovery.searchaccurate.DiscoverySearchAccurateActivity;

import me.drakeet.multitype.ItemViewProvider;

/**
 * 发现-搜索-结果-标题
 * <p>
 * Created by ChenTao(chentao7v@gmail.com) on 2017/4/13 0013 11:27
 */
public class DiscoverySearchTitleProvider extends ItemViewProvider<ArticleTitle, DiscoverySearchTitleProvider.ViewHolder> {

    private Activity mActivity;

    public DiscoverySearchTitleProvider(Activity activity) {
        mActivity = activity;
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View root = inflater.inflate(R.layout.item_discovery_search_title, parent, false);
        return new ViewHolder(root);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull final ArticleTitle article) {
        String title = article.getTitle();
        holder.tvTitle.setText(title);
        holder.tvShowMore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toSearchAccurateActivity(article);
            }
        });
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        private TextView tvTitle;
        private TextView tvShowMore;

        public ViewHolder(View itemView) {
            super(itemView);
            tvTitle = (TextView) itemView.findViewById(R.id.tv_title);
            tvShowMore = (TextView) itemView.findViewById(R.id.tv_show_more);
        }
    }

    private void toSearchAccurateActivity(ArticleTitle article) {
        AccurateContent content = new AccurateContent();
        content.setId(article.getId());
        content.setName(article.getTitle());
        content.setKeyword(article.getKeyword());
        DiscoverySearchAccurateActivity.actionStart(mActivity, content);
    }
}
